﻿using UnityEngine;
using System.Collections.Generic;
using TTGame;
/*
 * MagicBallManager
 * 法球管理
 *
 * 提供法球的创建和管理
 *
 * 依赖：
 * MagicBall
 *
 * ！不写重复代码！
 * @author TTun
 * @date 7/18/2017 8:23:55 PM
 */
namespace TTGameEngine {
public class MagicBallManager : MonoBehaviour {

    static MagicBallManager m_ins;
    public Transform m_rootTF;
    //<castID, MagicBall>
    public Dictionary<uint, MagicBall> magicBalls = new Dictionary<uint, MagicBall>();

    public static MagicBallManager Share() {
        if(m_ins == null) {
            m_ins = new GameObject("MagicBallManager").AddComponent<MagicBallManager>();
        }
        return m_ins;
    }

    public Transform GetRootTF() {
        if(m_rootTF == null) {
            m_rootTF = new GameObject("MagicBalls").transform;
        }
        return m_rootTF;
    }

    //创建方式 - prefab创建
    public MagicBall CreateMBWithPrefab(string MBname) {
        GameObject prefab = GameObjectLoader.Ins.GetPrefabObj(GameConstant.PrefabPath_Bullet + MBname) as GameObject;
        if(prefab == null) {
            DebugUtil.LogErrorFormat("[MBMgr] 子弹[{0}]没有找到.", MBname);
            return null;
        }
        GameObject go = ObjectRecycler.Instance.InstantiatePrefab(prefab, Vector3.zero, Quaternion.identity);
        go.SetActive(true);
        go.transform.parent = GetRootTF();
        MagicBall mb = go.GetComponent<MagicBall>();
        AddMagicBall(mb.GetGID(), mb);
        return mb;
    }

    public void AddMagicBall(uint mbGID, MagicBall mb) {
        magicBalls.Add(mbGID, mb);
    }
    public void RemoveMagicBall(MagicBall mb) {
        if(magicBalls.Remove(mb.GetGID()) == false) {
            mb.MagicBallLogError("删除子弹失败，没有找到子弹ID[{0}]。", mb.GetGID());
        }
    }

    public void DeactiveMagicBall(MagicBall mb, bool isRemove = true) {
        mb.DeactiveMagicBall(isRemove);
    }

    public void DeactiveAllMagicBall() {
        for(var ite = magicBalls.GetEnumerator(); ite.MoveNext();) {
            MagicBall mb = ite.Current.Value;
            DeactiveMagicBall(mb, false);
        }
        magicBalls.Clear();
    }
}
}
